package com.dongjiantong.app.controller.biz;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongjiantong.biz.model.FarmStableRecord;
import com.dongjiantong.biz.model.Stables;
import com.dongjiantong.biz.service.FarmStableRecordService;
import com.dongjiantong.biz.service.StablesService;
import com.dongjiantong.common.exceptions.HttpCode;
import com.dongjiantong.common.exceptions.OPServerException;
import com.dongjiantong.common.model.Response;
import com.dongjiantong.farmrecords.enums.FarmRecordsFileEnum;
import com.dongjiantong.farmrecords.model.FarmRecordsFile;
import com.dongjiantong.farmrecords.service.FarmRecordsFileService;
import com.dongjiantong.openplatform.annotations.OpenBean;
import com.dongjiantong.openplatform.annotations.OpenMethod;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.*;

/**
 * Created by ThinkPad on 2018/1/9.
 */
@Slf4j
@OpenBean
public class FarmStableRecordController {
    @Reference(version = "1.0.0")
    private FarmStableRecordService farmStableRecordService;
    @Reference(version = "1.0.0")
    private StablesService stablesService;
    @Reference(version = "1.0.0")
    private FarmRecordsFileService farmRecordsFileService;

    /**
     * 根据养殖场id查询 栏舍记录
     *
     * @param pageNum
     * @param pageSize
     * @param farmId
     * @return
     */
    @OpenMethod(key = "app.FarmStableRecord.selectByFarmId",
            paramNames = {"pageNum", "pageSize", "farmId"},
            httpMethods = RequestMethod.GET)
    public Response<PageInfo> selectByFarmId(Integer pageNum, Integer pageSize, Long farmId) {
        PageInfo pageInfo = farmStableRecordService.selectByPage(pageNum, pageSize, farmId);
        return Response.ok(pageInfo);
    }

    /**
     * 根据id查询栏舍记录
     * @param id
     * @return
     */
    @OpenMethod(key = "app.FarmStableRecord.findById",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET)
    public Response<Map> findById(Long id) {

        FarmStableRecord farmStableRecord = farmStableRecordService.selectByPrimaryKey(id);

        List<FarmRecordsFile> farmRecordsFileList = farmRecordsFileService.selectPicOrVideoListByExample(farmStableRecord.getId(), FarmRecordsFileEnum.STABLES.getCode(), FarmRecordsFileEnum.PHOTO.getCode());
        Map map = new HashMap();
        map.put("farmStableRecord",farmStableRecord);
        map.put("farmRecordsFileList",farmRecordsFileList);

        return Response.ok(map);

    }

    /**
     * 转舍
     *
     * @param farmStableRecord
     * @param farmRecordsFileList
     * @return
     */
    @OpenMethod(key = "app.FarmStableRecord.changStable",
            paramNames = {"farmStableRecord", "farmRecordsFileList"},
            httpMethods = RequestMethod.POST)
    @Transactional
    public Response<Map> changStable(FarmStableRecord farmStableRecord, List<Map> farmRecordsFileList) {
        try {
            //两个栏舍数量修改
            Stables oldStable = stablesService.selectByPrimaryKey(farmStableRecord.getFromStableId());
            Stables newStable = stablesService.selectByPrimaryKey(farmStableRecord.getToStableId());
            oldStable.setQuantity(oldStable.getQuantity() - farmStableRecord.getQuantity());
            newStable.setQuantity(newStable.getQuantity() + farmStableRecord.getQuantity());
            List<Stables> stables = new ArrayList<>();
            stables.add(oldStable);
            stables.add(newStable);
            stablesService.updateStablesSome(stables);
            // 创建栏舍记录
            farmStableRecord.setCreatedAt(new Date());
            Long recordId = farmStableRecordService.insert(farmStableRecord);
            //创建文件表
            if (farmRecordsFileList.size()!=0){
                List<FarmRecordsFile> farmRecordsFiles = new ArrayList<>();
                for (Map map : farmRecordsFileList) {
                    FarmRecordsFile farmRecordsFile = new FarmRecordsFile();
                    farmRecordsFile.setType1(FarmRecordsFileEnum.STABLES.getCode());
                    farmRecordsFile.setRelevance_id(recordId);
                    farmRecordsFile.setType2((Integer) map.get("type2"));
                    farmRecordsFile.setState((String) map.get("state"));
                    farmRecordsFile.setAddress((String) map.get("address"));
                    farmRecordsFile.setDate(new Date());
                    farmRecordsFiles.add(farmRecordsFile);

                }
                farmRecordsFileService.insertList(farmRecordsFiles);
            }

            Map map = new HashMap();
            map.put("data", "转舍成功");
            return Response.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            throw new OPServerException(HttpCode.DATA_UPDATE_FAIL, "转舍失败请稍后再试");
        }

    }
}
